package com.cql.service.business.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cql.common.constant.FoodCircleConstant;
import com.cql.common.core.DO.PictureDO;
import com.cql.common.core.DTO.PictureDTO;
import com.cql.common.core.base.BaseEntity;
import com.cql.common.exception.file.FileException;
import com.cql.common.exception.file.InvalidExtensionException;
import com.cql.common.utils.StringUtils;
import com.cql.mapper.PictureMapper;
import com.cql.service.business.PictureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cql
 */
@Service("pictureService")
public class PictureServiceImpl extends ServiceImpl<PictureMapper, PictureDO> implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public PictureDTO insertPicture(PictureDO pictureDO) {
        if (StringUtils.isNotNull(pictureDO) && StringUtils.isNotEmpty(pictureDO.getSrc())) {
            pictureMapper.insert(pictureDO);
            PictureDTO picture = new PictureDTO();
            BeanUtils.copyProperties(pictureDO, picture);
            return picture;
        }
        return null;
    }

    @Override
    public void deleteImgs(List<Long> removeImg) {
        if (StringUtils.isNotEmpty(removeImg)) {
            this.removeByIds(removeImg);
        }
    }

    @Override
    public void deleteImgsBySrc(List<String> imgs) {
        if (StringUtils.isNotEmpty(imgs)) {
            for (String img : imgs) {
                QueryWrapper<PictureDO> wrapper = new QueryWrapper<>();
                wrapper.eq("src", img);
                pictureMapper.delete(wrapper);
            }
        }
    }

    @Override
    public List<PictureDTO> listPicture(String covers) {
        List<PictureDTO> result = new ArrayList<>();

        if (StringUtils.isNotEmpty(covers)) {
            String[] split = covers.split(FoodCircleConstant.SEPARATE_CHAR);
            for (String s : split) {
                PictureDO pictureDO = pictureMapper.selectById(Long.parseLong(s));
                if (StringUtils.isNotNull(pictureDO)) {
                    PictureDTO p = new PictureDTO();
                    BeanUtils.copyProperties(pictureDO, p);
                    result.add(p);
                }
            }
        }
        return result;
    }

    @Override
    public PictureDTO getPictureBySrc(String img) {
        PictureDTO p = new PictureDTO();

        if (StringUtils.isNotEmpty(img)) {
            QueryWrapper<PictureDO> wrapper = new QueryWrapper<>();
            wrapper.eq("src", img);
            PictureDO pictureDO = pictureMapper.selectOne(wrapper);
            if (StringUtils.isNotNull(pictureDO)) {
                BeanUtils.copyProperties(pictureDO, p);
            }
        }
        return p;
    }
}